package com.coder.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class SecurityImage {
    
    //according to the security code, generate a BufferedImage
    public static BufferedImage createImage(String securityCode){
        
        int codeLength=securityCode.length();
        
        //font size
        int fSize = 15;
        int fWidth = fSize + 1;
        
        //width of image
        int width = codeLength * fWidth + 6 ;
        int height = fSize * 2 + 1;
        
        //image
        BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g=image.createGraphics();
        
        //set the background color
        g.setColor(Color.WHITE);
        //fill background
        g.fillRect(0, 0, width, height);
        
        //set the border color
        g.setColor(Color.LIGHT_GRAY);
        //set font style
        g.setFont(new Font("Arial", Font.BOLD, height - 2));
        //draw rect
        g.drawRect(0, 0, width - 1, height -1);
        
        
        //draw noise point
        Random rand = new Random();
        //set the color of noise point
        g.setColor(Color.LIGHT_GRAY);
        for(int i = 0;i < codeLength * 6;i++){
            int x = rand.nextInt(width);
            int y = rand.nextInt(height);
            //draw rect of 1*1
            g.drawRect(x, y, 1, 1);
        }
        
        //draw security code
        int codeY = height - 10;  
        //set the font color and style
        g.setColor(new Color(19,148,246));
        g.setFont(new Font("Georgia", Font.BOLD, fSize));
        for(int i = 0; i < codeLength;i++){
            g.drawString(String.valueOf(securityCode.charAt(i)), i * 16 + 5, codeY);
        }
        //close graphics
        g.dispose();
        
        return image;
    }
    
    //return the image stream
    public static ByteArrayInputStream getImageAsInputStream(String securityCode){
        
        BufferedImage image = createImage(securityCode);
        return convertImageToStream(image);
    }
    
    //not understand
    private static ByteArrayInputStream convertImageToStream(BufferedImage image){
        
        ByteArrayInputStream inputStream = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
        try {
            jpeg.encode(image);
            byte[] bts = bos.toByteArray();
            inputStream = new ByteArrayInputStream(bts);
        } catch (ImageFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputStream;
    }
}